<style>
  .picImg {
    width: 40px;
    height: 40px;
  }
  .el-table thead {
    color: #333;
  }
</style>
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="infoTime.startTime"
        type="datetime"
        placeholder="查询开始日期"
        value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
      -
      <el-date-picker
        v-model="infoTime.endTime"
        type="datetime"
        placeholder="查询结束日期"
        value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
      <el-select v-model="infoTime.status" placeholder="请选择状态">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button type="primary" @click="getList">查询</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row
              @row-click="detail" :header-cell-style="{background:'#409EFF'}">

      <!--<el-table-column fixed align="center" min-width="100" label="表单ID" prop="id"/>-->
      <el-table-column align="center" min-width="100" label="表单编号" prop="formNo"/>
      <el-table-column align="center" min-width="100" label="上传时间" prop="addTime"/>
      <el-table-column align="center" min-width="100" label="保险公司名称" prop="insuranceID"/>
      <el-table-column align="center" min-width="100" label="保代公司名称" prop="insuragentID"/>
      <el-table-column align="center" min-width="100" label="保理公司名称" prop="factoringID"/>
      <el-table-column align="center" min-width="100" label="保单总数" prop="billCount"/>
      <el-table-column align="center" min-width="100" label="手续费总额 (元)" prop="amount"/>
      <el-table-column align="center" min-width="100" label="表单状态" prop="status">
        <template slot-scope="scope">{{ scope.row.status | statesFilter }}</template>
      </el-table-column>
      <el-table-column align="center" min-width="100" label="保代公司处理时间" prop="advanceTime"/>
      <el-table-column align="center" min-width="100" label="需要垫资保单数" prop="advanceCount"/>
      <el-table-column align="center" min-width="100" label="需要垫资金额 (元)" prop="advanceAmount"/>
      <el-table-column align="center" min-width="100" label="垫资支付时间" prop="advanceTime"/>
      <el-table-column align="center" min-width="100" label="回款总额 (元)" prop="repayAmount"/>
      <el-table-column align="center" min-width="100" label="回款时间" prop="repayTime"/>
      <el-table-column align="center" min-width="100" label="垫资利息费用 (元)" prop="interest"/>
      <el-table-column align="center" min-width="100" label="上传备注" prop="remarkUp"/>
      <el-table-column align="center" min-width="100" label="垫资申请备注" prop="remarkAdv"/>
      <el-table-column fixed="right" align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" v-if="scope.row.status==0"
                     @click.stop="handleEdit(scope.row)">垫资
          </el-button>
          <el-button type="primary" size="mini" v-if="scope.row.status==1"
                     @click.stop="handleEditNew(scope.row)">重新垫资
          </el-button>
          <el-button type="primary" size="mini" v-if="scope.row.status==4"
                     @click.stop="interest1(scope.row)">支付垫资利息
          </el-button>
          <el-button type="primary" size="mini" v-if="scope.row.status==5"
                     @click.stop="balanceCash(scope.row)">余款提款
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize"
                @pagination="getList"/>
    <!--详情表单-->
    <el-dialog :visible.sync="editDialogVisible" :title="textMap[dialogStatus]" :close-on-click-modal="false">
      <el-table v-loading="listLoading1" :data="list1" element-loading-text="正在查询中。。。" border fit highlight-current-row
                :header-cell-style="{background:'#409EFF'}">

        <!--<el-table-column align="center" min-width="100" label="表单ID" prop="id"/>-->
        <el-table-column align="center" min-width="100" label="保单号或合同号" prop="billNo"/>
        <el-table-column align="center" min-width="100" label="保单金额" prop="billAmount"/>
        <el-table-column align="center" min-width="100" label="代理手续费" prop="fee"/>
        <el-table-column align="center" min-width="100" label="代理人身份证号" prop="agentID"/>
        <el-table-column align="center" min-width="100" label="代理人编码" prop="agentNo"/>
        <el-table-column align="center" min-width="100" label="是否需要垫资" prop="isAdvance">
          <template slot-scope="scope">{{ scope.row.isAdvance | typeFilter   }}</template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editDialogVisible = false">确定</el-button>
      </div>
    </el-dialog>
    <!--垫资表单-->
    <el-dialog :visible.sync="editDialogVisible1" :title="textMap[dialogStatus]" :close-on-click-modal="false">
      <el-form ref="editForm" :rules="editFormRules" :model="editForm" status-icon label-position="left"
               label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="表单ID" prop="id">
          <el-input disabled v-model="editForm.id" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="是否全部申请垫资" prop="allAdvance">
          <el-radio-group v-model="editForm.allAdvance">
            <el-radio :label="0">非</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editForm.remark" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="明细列表" prop="billFile">
          <!--<el-input v-model="editForm.billFile" auto-complete="off"/>-->
          <el-upload
            class="upload-demo"
            ref="upload"
            action="string"
            :before-upload="beforeUploadFile"
            :http-request="doUpload"
            :file-list="fileList"
            :limit="1"
            name="filename"
            :auto-upload="false">
            <el-button size="small" plain>选择文件</el-button>
            <div slot="tip" class="el-upload__tip">只能上传xlsx(Excel2007)文件，且不超过10M</div>
            <!--<el-button slot="trigger" size="small" type="primary">选取文件</el-button>-->
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible1 = false">取消</el-button>
        <el-button type="primary" @click="doCreate">确定</el-button>
      </div>
    </el-dialog>
    <!--重新垫资表单-->
    <el-dialog :visible.sync="editDialogVisible2" :title="textMap[dialogStatus]" :close-on-click-modal="false">
      <el-form ref="editForm1" :rules="editFormRules1" :model="editForm1" status-icon label-position="left"
               label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="表单ID" prop="id">
          <el-input disabled v-model="editForm1.id" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="是否全部申请垫资" prop="allAdvance">
          <el-radio-group v-model="editForm1.allAdvance">
            <el-radio :label="0">非</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editForm1.remark" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="明细列表" prop="billFile">
          <!--<el-input v-model="editForm.billFile" auto-complete="off"/>-->
          <!--<el-upload-->
            <!--class="upload-demo"-->
            <!--ref="upload"-->
            <!--action="string"-->
            <!--:before-upload="beforeUploadFile"-->
            <!--:http-request="doUpload"-->
            <!--:file-list="fileList"-->
            <!--:limit="1"-->
            <!--name="filename"-->
            <!--:auto-upload="false">-->
            <!--<el-button size="small" plain>选择文件</el-button>-->
            <!--<div slot="tip" class="el-upload__tip">只能上传xlsx(Excel2007)文件，且不超过10M</div>-->
            <!--&lt;!&ndash;<el-button slot="trigger" size="small" type="primary">选取文件</el-button>&ndash;&gt;-->
          <!--</el-upload>-->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible2 = false">取消</el-button>
        <el-button type="primary" @click="doCreate1">确定</el-button>
      </div>
    </el-dialog>
    <!--表单余额提款-->
    <el-dialog :visible.sync="editDialogVisible3" :title="textMap[dialogStatus]" :close-on-click-modal="false">
      <el-form ref="editForm2" :rules="editFormRules2" :model="editForm2" status-icon label-position="left"
               label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="表单ID" prop="id">
          <el-input disabled v-model="editForm2.id" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="保代公司收款账号" prop="account">
          <el-input v-model="editForm2.account" auto-complete="off"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible3 = false">取消</el-button>
        <el-button type="primary" @click="doCreate2">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {doGet, doPost} from '@/api'
  import {cities} from '@/utils/base'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

  const FETCH_PAGE = 'bdagentfee.html'
  const FETCH_URL = {
    LIST: `${FETCH_PAGE}?op=list`,
    CREATE: `${FETCH_PAGE}?op=billlist`,
    EDIT: `${FETCH_PAGE}?op=formup`,
    DELETE: `${FETCH_PAGE}?op=formreup`,
    IMPORT: `${FETCH_PAGE}?op=interest`,
    PAY: `${FETCH_PAGE}?op=pay`
  }
  export default {
    name: 'bdagentfee',
    components: {Pagination},
    filters: {
      statesFilter(type) {
        return {
          '0': '表单已上传',
          '1': '保代公司垫资已申请',
          '2': '保理公司已接收',
          '3': '保理公司垫资已支付',
          '4': '保险公司已回款',
          '5': '垫资利息已支付',
          '6': '保代公司已取款',
        }[type]
      },
      typeFilter(type) {
        return {
          '0': '待确认',
          '1': '不需要',
          '2': '需要',
        }[type]
      }
    },
    data() {
      return {
        startTime: '',
        endTime: '',
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        options: [{
          value: '',
          label: '请选择状态'
        }, {
          value: '0',
          label: '表单已上传'
        }, {
          value: '1',
          label: '保代公司垫资已申请'
        }, {
          value: '2',
          label: '保理公司已接收'
        }, {
          value: '3',
          label: '保理公司垫资已支付'
        }, {
          value: '4',
          label: '保险公司已回款'
        }, {
          value: '5',
          label: '垫资利息已支付'
        }, {
          value: '6',
          label: '保代公司已取款'
        }],
        list: [],
        list1:[],
        total: 0,
        listLoading: true,
        listLoading1: true,
        infoTime:{
          startTime:undefined,
          endTime:undefined,
          status: undefined,
        },
        dialogStatus:undefined,
        textMap: {
          datail: '表单明细列表',
          create: '表单垫资确认',
          createNew:'表单垫资重新确认',
          balanceRmb:'余额提款',
        },
        listQuery: {
//                    page: 1,
//                    pageSize: 20
        },
//        垫资
        editForm: {
          id: undefined,
          allAdvance: undefined,
          remark: undefined,
          billFile: undefined,
        },
        editFormRules: {
          id: [{required: true, message: '表单ID不允许为空', trigger: 'blur'}],
          allAdvance: [{required: true, message: '是否全部申请垫资不允许为空', trigger: 'blur'}],
        },
//        重新垫资
        editForm1: {
          id: undefined,
          allAdvance: undefined,
          remark: undefined,
          billFile: undefined,
        },
        editFormRules1: {
          id: [{required: true, message: '表单ID不允许为空', trigger: 'blur'}],
          allAdvance: [{required: true, message: '是否全部申请垫资不允许为空', trigger: 'blur'}],
        },
//        余额提款
        editForm2: {
          id: undefined,
          account: undefined,
        },
        editFormRules2: {
          id: [{required: true, message: '表单ID不允许为空', trigger: 'blur'}],
          account: [{required: true, message: '保代公司收款账号不允许为空', trigger: 'blur'}],
        },
        editDialogVisible: false,
        editDialogVisible1: false,
        editDialogVisible2: false,
        editDialogVisible3: false,
        fileList:[],
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true;
        doGet(FETCH_URL.LIST, this.infoTime).then(response => {
          this.list = response.rows;
          this.total = response.rows.length;
          this.listLoading = false
        }).catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false
        })
      },
      detail(row) {
        this.dialogStatus = 'datail'
        this.editDialogVisible = true
        this.listLoading1=true
//        console.log(row.id)
        doGet(FETCH_URL.CREATE, {id:row.id}).then(response => {
          this.list1=response.rows
//          console.log(response.rows);
          this.listLoading1=false
        }).catch(() => {
          this.total = 0;
          this.listLoading1=false
        })
      },
//      垫资确认按钮
      handleEdit(row) {
        this.editDialogVisible1=true
        this.dialogStatus = 'create'
        this.editForm = {
          id: undefined,
          allAdvance: undefined,
          remark: undefined,
          billFile: undefined,
        }
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
        this.editForm.id = row.id
      },
//      垫资确认
      doCreate() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            let sendData = {
              ...this.editForm,
            }
            doGet(FETCH_URL.EDIT, sendData)
              .then(response => {
                this.getList()
                this.editDialogVisible1 = false
                this.$notify.success({
                  title: '成功',
                  message: '垫资确认成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
//      重新垫资确认
      handleEditNew(row){
        this.editDialogVisible2=true
        this.dialogStatus = 'createNew'
        this.editForm1 = {
          id: undefined,
          allAdvance: undefined,
          remark: undefined,
          billFile: undefined,
        }
          this.$nextTick(() => {
            this.$refs['editForm1'].clearValidate()
          })
        this.editForm1.id = row.id
      },
      doCreate1() {
        this.$refs['editForm1'].validate(valid => {
          if (valid) {
            let sendData = {
              ...this.editForm1,
            }
            doGet(FETCH_URL.DELETE, sendData)
              .then(response => {
                this.getList()
                this.editDialogVisible2 = false
                this.$notify.success({
                  title: '成功',
                  message: '重新垫资确认成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
//      余额提款
      balanceCash(row){
        this.editDialogVisible3=true
        this.dialogStatus = 'balanceRmb'
        this.editForm2 = {
          id: undefined,
          account: undefined,
        }
          this.$nextTick(() => {
            this.$refs['editForm2'].clearValidate()
          })
        this.editForm2.id = row.id
      },
      doCreate2() {
        this.$refs['editForm2'].validate(valid => {
          if (valid) {
            let sendData = {
              ...this.editForm2,
            }
            doGet(FETCH_URL.PAY, sendData)
              .then(response => {
                this.getList()
                this.editDialogVisible3 = false
                this.$notify.success({
                  title: '成功',
                  message: '余额提款成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
//      利息支付
      interest1(row){
        doGet(FETCH_URL.IMPORT, {id:row.id})
          .then(response => {
            this.getList()
            this.$notify.success({
              title: '成功',
              message: '垫资利息支付成功'
            })
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.message
            })
          })
      },
      beforeUploadFile(file) {
        let extension = file.name.substring(file.name.lastIndexOf('.') + 1)
        let size = file.size / 1024 / 1024
        if (extension !== 'xlsx' && extension !== 'xls') {
          this.$notify.warning({
            title: '警告',
            message: `只能上传Excel（即后缀是.xlsx或.xls）的文件`
          });
          return false
        }
      },
      doUpload(param) {
        console.log(param.file)
//        console.log('upload==>')
//        let fd = new FormData()
//        fd.append('filename', param.file)//传文件
      }
    }
  }
</script>
